package k8sClient

import (
	"github.com/sirupsen/logrus"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
)

// NewOutsideClusterClientSet 创建一个集群外的clientset
// clusterConnectFile是kubeconfig文件的路径，如果为""，则指定为clientcmd.RecommendedHomeFile
func NewOutsideClusterClientSet(clusterConnectFile string) (*kubernetes.Clientset, error) {
	if clusterConnectFile == "" {
		clusterConnectFile = clientcmd.RecommendedHomeFile
	}
	config, err := clientcmd.BuildConfigFromFlags("", clusterConnectFile)
	if err != nil {
		logrus.Error("[NewOutSideClientSet]-无法创建clientset所需的config")
		return nil, err
	}

	cs, err := kubernetes.NewForConfig(config)
	if err != nil {
		logrus.Error("[NewOutSideClientSet]-无法创建clientset")
		return nil, err
	}
	return cs, nil
}

// NewInsideClusterClientSet 创建一个集群内的clientset
func NewInsideClusterClientSet() (*kubernetes.Clientset, error) {
	config, err := rest.InClusterConfig()
	if err != nil {
		logrus.Errorf("[NewInsideClusterClientSet]-创建incLusterConfig失败")
		return nil, err
	}
	cs, err := kubernetes.NewForConfig(config)
	if err != nil {
		logrus.Error("[NewOutSideClientSet]-无法创建clientset")
		return nil, err
	}
	return cs, nil
}
